﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BehaviorSystem
{
    public class LoopNode : DecoratorNode
    {
        public int LoopNum = 0;

        public int curNum;

        public BTNodeState childNodeState;

        public LoopNode(IBehaviorAgent agent, int loopNum) : base(agent)
        {
            LoopNum = loopNum;
            CurState = BTNodeState.Running;
        }

        public override BTNodeState HandleDecorator()
        {
            if (CurState == BTNodeState.Running)
            {
                if (curNum < LoopNum)
                {
                    if (childNodeState == BTNodeState.Success || childNodeState == BTNodeState.Failure)
                    {
                        curNum++;
                    }

                    for (int i = 0; i < ChildrenNodes.Count; i++)
                    {
                        childNodeState = ChildrenNodes[i].ExcuteNode();
                    }

                }
                else
                {
                    CurState = BTNodeState.Success;
                }
            }

            return CurState;
        }
    }
}